package com.fun.demo.fun.kayang;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexDemo {
    public static void main(String[] args) {
        String sql = "SELECT * FROM users WHERE ? or name =? AND age > ?";

        int placeholderCount = countPlaceholders(sql);
        System.out.println("Number of placeholders: " + placeholderCount);

    }

    public static int countPlaceholders(String sql) {
        // 正则表达式匹配不在字符串内的问号
        String regex = "[^a-zA-Z0-9_]\\?";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(sql);

        int count = 0;
        while (matcher.find()) {
            count++;
        }

        return count;


    }
}
